package com.hgw.easyexcel3_demo;

import com.alibaba.excel.EasyExcel;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Data time:2022/8/30 14:03
 * Author:TQ-3038-HuangGongWei
 */
@SpringBootTest
public class WriteDemo {


    private final String PATH = "/Users/gwh/Documents/data/demo/easy-excel-demo/easyexcel3_demo/src/main/resources/warehouse/";

    @Test
    public void noModelWrite() {
        // 写法1
        String fileName = PATH + "noModelWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName).head(head()).sheet("sheet1").doWrite(data());
    }

    @Test
    public void customImageCellWriteHandlerTest() {
        // 写法1
        String fileName = PATH + "noModelWrite" + System.currentTimeMillis() + ".xlsx";
        List<Integer> imageColumnIndexs = new ArrayList<>();
        imageColumnIndexs.add(0);
        imageColumnIndexs.add(4);
        imageColumnIndexs.add(8);
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName).head(head()).sheet("sheet1").doWrite(data());
    }

    private List<List<String>> head() {
        List<List<String>> list = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            List<String> head0 = new ArrayList<>();
            head0.add("标题"+i);
            list.add(head0);
        }
        return list;
    }

    private List<List<Object>> data() {
        List<List<Object>> datas = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            List<Object> list = new ArrayList<>();
            for (int j = 0; j <10 ; j++) {
                if((i==1&&j==5)||(i==2&&j==0)||(i==3&&j==7)){
                    URL image = null;
                    try {
                        image = new URL("https://img.linhuiba.com/FvXO_WtI7CI2z6-kx2BbD93fNdWS-linhuiba_watermark");
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    // 这里可以根据需求传入File,InputStream,byte[],URL各种类型,如果要传String类型的话,就需要考虑和普通字符串的区分了
                    list.add(image);
                }else {
                    list.add("内容"+i+"-"+j);
                }
            }
            datas.add(list);
        }
        return  datas;
    }


}
